package com.dao;

import com.javabean.Customer;
import com.util.BaseDao;

import java.util.ArrayList;
import java.util.List;


public class CustomerDao extends BaseDao {
    /**
     * 用途：查询数据库返回所有客户对象
     * 返回：集合
     */
    public List<Customer> listCustomer() throws Exception {
        ArrayList<Customer> list = query(Customer.class, "select * from t_customer");
//        query()
        return list;
    }

    public void insertCustomer(Customer customer) throws Exception {
        int row = update("insert into t_customer(id,NAME,gender,age,salary,phone,password,user) values (?,?,?,?,?,?,?,?);",
                customer.getId(), customer.getNAME(), customer.getGender(), customer.getAge(), customer.getSalary(), customer.getPhone(), customer.getPassword(), customer.getUser());
    }

    public int updateCustomer(Customer customer) throws Exception {
        return update("update t_customer set name = ? , gender = ? , age = ? ," +
                        "salary = ? , phone = ?,user=?,password=? where id = ? ;", customer.getNAME(), customer.getGender(),
                customer.getAge(), customer.getSalary(), customer.getPhone(),customer.getUser(), customer.getPassword(),customer.getId());
    }

    public int deleteCustomer(int id) throws Exception {
        return update("delete from t_customer where id =?", id);
    }

    public ArrayList<Customer> getCustomer(String user) throws Exception {
        ArrayList<Customer> list1 = query(Customer.class, "select * from t_customer where user=?",user);
//        System.out.println(list1);
        return list1;
    }

   /* public static void main(String[] args) {
        Object[] user = new String[]{"lilin"};
        try {
            ArrayList<Customer> customer = query(Customer.class, "select * from t_customer where user=?", user);
            if (customer != null) {
                // 假设Customer类已正确实现toString方法，且不包含敏感信息
                System.out.println(customer);
            } else {
                System.out.println("No customer found for the given user.");
            }
        } catch (Exception e) {
            e.printStackTrace(); // 在实际应用中，应更详细地处理异常，如记录日志或提供用户友好的反馈
        }
        // 移除或解释被注释掉的代码
    }*/



}
